package com.gdth.bank.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gdth.bank.BankApplication;
import com.gdth.bank.R;
import com.gdth.bank.adapter.DbJjDjListAdapter;
import com.gdth.bank.adapter.DbJjDjListAdapter.OnCjjlDetailClickListener;
import com.gdth.bank.bean.DbjjItem;
import com.gdth.bank.bean.TAppGmjjdjJc;
import com.gdth.bank.presenter.SynchronizePresenter;
import com.gdth.bank.view.GmJjDjView;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseActivity;
import com.gdth.database.bean.TAppYh;
import com.gdth.model.Item;
import com.gdth.widget.dialog.BaseToast;
import com.gdth.widget.pulltorefresh.library.PullToRefreshBase;
import com.gdth.widget.pulltorefresh.library.PullToRefreshListView;
import com.gdth.widget.pulltorefresh.library.PullToRefreshBase.Mode;
import com.gdth.widget.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.gdth.widget.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;

public class BjjDjActivity extends BaseActivity implements GmJjDjView{
	@ViewInject(id=R.id.header_label_title) TextView txt_header_title;
	@ViewInject(id=R.id.header_btn_left, click="finishClick") TextView txt_header_left;
    @ViewInject(id=R.id.header_btn_right, click="addClick") TextView txt_header_right;
//    @ViewInject(id=R.id.dbjjdj_query, click="queryClick") TextView txt_query;
//    @ViewInject(id=R.id.dbjjdj_editTextNote) EditText edit_query;
   	@ViewInject(id=R.id.dbjj_listView) PullToRefreshListView listView;
   	ListView mListView;
   	List<DbjjItem> listData;
   	DbJjDjListAdapter mAdapter;
   	/** 提示框 */
    ProgressDialog mProgressDialog;
	SynchronizePresenter presenter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dbjjdj);
		initView();
		init();
	}
	@SuppressLint("ResourceAsColor")
	private void initView() {
		txt_header_title.setText(getResources().getString(R.string.param67));
		 mProgressDialog = new ProgressDialog(this);
		 mProgressDialog.setCancelable(false);
		 presenter = new SynchronizePresenter(this);
	        mListView = listView.getRefreshableView();
	        mListView.setDivider(new ColorDrawable(R.color.line_color));
	        mListView.setDividerHeight(1);
	        
	        listView.setOnRefreshListener(new OnRefreshListener<ListView>() {
				@Override
				public void onRefresh(PullToRefreshBase<ListView> refreshView) {
					String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
							DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
					
					refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
					if(refreshView.isHeaderShown()){
						TAppYh user = BankApplication.mInstance.getCurrentUser();
						String  time;
						JSONObject content  =new JSONObject();
						int listDataSize=listData.size();
						if(listDataSize>0){
	                    	 time =listData.get(0).getCjsj();
	                    	  }
							else{
	                    		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	     						 time = sdf.format(new Date());
	                    	   }
						try {
							content.put("sfz", user.getSfz());
							content.put("time", time);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						presenter.getbjjdjUpList(content.toString());
						Toast.makeText(BjjDjActivity.this, getResources().getString(R.string.param68), Toast.LENGTH_LONG).show();
					}else{
						TAppYh user = BankApplication.mInstance.getCurrentUser();
						int listDataSize=listData.size();
						if(listDataSize>0){
                  	  String time =listData.get(listDataSize-1).getCjsj();
                  	  JSONObject content  =new JSONObject();
                        try {
                        	content.put("sfz", user.getSfz());
                        	content.put("time", time);
                		} catch (JSONException e) {
                			// TODO Auto-generated catch block
                			e.printStackTrace();
                		}
                        presenter.getbjjdjOffList(content.toString());
                        }else{
                        	listView.onRefreshComplete();
                        }
						Toast.makeText(BjjDjActivity.this, getResources().getString(R.string.param69), Toast.LENGTH_LONG).show();
					}
				}
			});

	        listView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
				@Override
				public void onLastItemVisible() {
					
				}
			});
	        
	        listData = new ArrayList<DbjjItem>();
	        mAdapter = new DbJjDjListAdapter(this, listData);
	        listView.setAdapter(mAdapter);
	         listView.setMode(Mode.BOTH);
	         mAdapter.setOnCjjlDetailClickListener(new OnDetailClickListener());
	        registerForContextMenu(mListView);
	}
	class OnDetailClickListener implements OnCjjlDetailClickListener {
		@Override
		public void clickListener(int position) {
			detail(position);
		}

		private void detail(int position) {
			// TODO Auto-generated method stub
			int id=listData.get(position).getId();
			Intent intent=new Intent();
			intent.putExtra("id", id+"");
			intent.putExtra("zhblr", listData.get(position).getZhblr());
			intent.putExtra("spzt", listData.get(position).getSpzt()+"");
			intent.putExtra("lczt", listData.get(position).getLcZt()+"");
			intent.setClass(BjjDjActivity.this, BjjDjXqActivity.class);
			startActivity(intent);
			
		}
	}
	public void init(){
		TAppYh user = BankApplication.mInstance.getCurrentUser();
		JSONObject content  =new JSONObject();
		try {
			content.put("sfz", user.getSfz());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		presenter.getbjjdjList(content.toString());
	}
	@Override
	public void showProgressDialog(String msg) {
		// TODO Auto-generated method stub
		mProgressDialog.setMessage(msg);
		mProgressDialog.show();
	}
	@Override
	public void hideProgressDialog() {
		// TODO Auto-generated method stub
		mProgressDialog.hide();
	}
	@Override
	public void showMessage(String msg) {
		// TODO Auto-generated method stub
		BaseToast.getInstance().ShowSystemToast(this, msg);
	}
	@Override
	public void getBmList(List<Item> arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void scsuccess() {
		// TODO Auto-generated method stub
//		Intent intent= new Intent();
//		intent.setClass(BjjDjActivity.this, DbJjDjActivity.class);
//		startActivity(intent);
	}
	@Override
	public void getDbList(List<DbjjItem> arg0) {
		// TODO Auto-generated method stub
		for(int i=0;i<arg0.size();i++){
			 DbjjItem  mDbjjItem= new DbjjItem();
			mDbjjItem.setJjsj(arg0.get(i).getJjsj());
			mDbjjItem.setJssj(arg0.get(i).getJssj());
			mDbjjItem.setGw(arg0.get(i).getGw());
			mDbjjItem.setId(arg0.get(i).getId());
	        mDbjjItem.setSlzt(arg0.get(i).getSlzt());
	        mDbjjItem.setSpzt(arg0.get(i).getSpzt());
	        mDbjjItem.setFqrMc(arg0.get(i).getFqrMc());
	        mDbjjItem.setZhblr(arg0.get(i).getZhblr());
	        mDbjjItem.setZhblsj(arg0.get(i).getZhblsj());
	        mDbjjItem.setSpyj(arg0.get(i).getSpyj());
	        mDbjjItem.setCjsj(arg0.get(i).getCjsj());
	        mDbjjItem.setJjrmc(arg0.get(i).getJjrmc());
	        mDbjjItem.setJsrmc(arg0.get(i).getJsrmc());
	        listData.add(mDbjjItem);
		}
		listView.onRefreshComplete();
		mAdapter.notifyDataSetChanged();
		

	}
	@Override
	public void getdbjjdjList(List<TAppGmjjdjJc> arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void getGwList(List<Item> arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void getdjJson(List<TAppGmjjdjJc> arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void deleteJson(String arg0) {
		// TODO Auto-generated method stub
	}

}
